﻿using EM.FrameWork;
using EM.FrameWork.Paging;
using EM.OA.BLL;
using EM.OA.DataModel.DBModel;
using EM.OA.Portal.Models;
using EM.OA.WebCommon.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace EM.OA.Portal.Controllers
{
    public class DepartmentController : BaseAuthController
    {
        //
        // GET: /Department/
        DepartmentBLL _departmentBLL = new DepartmentBLL();
        public ActionResult Index()
        {
            if (_departmentBLL.GetList().Where(p => p.DepartmentParentID == new Guid() || !p.DepartmentParentID.HasValue).Count() < 1)
            {
                EMOA_Department rootDepartment = new EMOA_Department();
                rootDepartment.DepartmentID = Guid.NewGuid();
                rootDepartment.DepartmentName = "root";
                rootDepartment.DepartmentParentID = new Guid();
                _departmentBLL.Add(rootDepartment);
            }
            return View();
        }

        public ActionResult GetTreeList()
        {
            List<EMOA_Department> list = _departmentBLL.GetList();
            List<TreeNode> nodeList = new List<TreeNode>();
            list.ForEach(p => {
                nodeList.Add(new TreeNode(p.DepartmentID, p.DepartmentParentID.Value, p.DepartmentName));
            });

            TreeNodeHelper helper = new TreeNodeHelper();
            TreeNode treeNode = new TreeNode();
            treeNode = helper.GenerateTreeRoot(nodeList);

            string result = treeNode.ToJsonTreeString();
            return Content(result);
        }

        public ActionResult GetTreeList2(Guid parentId)
        {
             List<EMOA_Department> list = _departmentBLL.GetList().Where(p=>p.DepartmentParentID==parentId).ToList();
                   string  resultStr = "";
                     resultStr += "[";
                     foreach (EMOA_Department item in list)
                     {
                         resultStr += "{";
                         resultStr += string.Format("\"id\": \"{0}\", \"text\": \"{1}\", \"iconCls\": \"icon-ok\", \"state\": \"closed\"", item.DepartmentID.ToString(), item.DepartmentName);
                         resultStr += "},";
                     }
                     resultStr = resultStr.Substring(0, resultStr.Length - 1);                   
                     resultStr += "]";
             return Content(resultStr);
        }

        public JsonResult GetList(string page, string rows)
         {
             string _parentID = GetParam("p_DepartmentParentID");
             List<object> result = new List<object>();
             PagedList<EMOA_Department> list = _departmentBLL.GetPagedList(Convert.ToInt32(page), Convert.ToInt32(rows),
                 p => (string.IsNullOrEmpty(_parentID) || p.DepartmentParentID == new Guid(_parentID) || p.DepartmentID == new Guid(_parentID)),
                 p=>p.CreateTime,false);

             list.ForEach(p =>
             {
                 string _departmentParentName = string.Empty;
                 string _manageUserName = string.Empty; 
                 if (p.DepartmentParentID.HasValue&&p.DepartmentParentID.Value!= new Guid())
                 {
                     _departmentParentName = _departmentBLL.GetEntity(p.DepartmentParentID.Value).DepartmentName;
                 }
                 if (p.ManageUserID.HasValue)
                 {
                     UserBLL _userBll = new UserBLL();
                     _manageUserName = _userBll.GetEntity(p.ManageUserID.Value).EmployeeName;
                 }
                 result.Add(new { DepartmentID = p.DepartmentID, DepartmentName = p.DepartmentName, DepartmentParentName = _departmentParentName, ManageUserID = _manageUserName, Telephone = p.Telephone, Address = p.Address });
             });

             return ToJson(result, list.TotalItemCount);

        }

        public ActionResult GetEntity(Guid id)
        {
            var data = _departmentBLL.GetEntity(id); ;
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        //
        // POST: /Task/Create

        [HttpPost]
        public ActionResult Create(DepartmentModels model)
        {
            try
            {
                EMOA_Department ep = model.ToEntity<EMOA_Department>();
                ep.DepartmentID = Guid.NewGuid();
                if (_departmentBLL.Add(ep))
                {
                    return Content("OK");
                }
                else
                {
                    return Content("失败");
                }
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }
        
         //POST: /Task/Edit/5

        [HttpPost]
        public ActionResult Edit(DepartmentModels model)
        {
            try
            {
                EMOA_Department entity = _departmentBLL.GetEntity(model.DepartmentID);
                entity = model.ToEntity<EMOA_Department>(entity);
                if (_departmentBLL.Update(entity))
                    return Content("OK");
                else
                    return Content("失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

        //
        // POST: /Task/Delete/5

        [HttpPost]
        public ActionResult Delete(string id)
        {
            try
            {
                if (_departmentBLL.Delete(Guid.Parse(id)))
                    return Content("OK");
                else
                    return Content("失败");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

    }
}
